package com.gdust.gkyx.activity.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdust.gkyx.activity.service.ActivityInfoService;
import com.gdust.gkyx.common.result.Result;
import com.gdust.gkyx.model.activity.ActivityInfo;
import com.gdust.gkyx.model.product.SkuInfo;
import com.gdust.gkyx.vo.activity.ActivityRuleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * ClassName: ActivityInfoController
 * Package: com.gdust.gkyx.activity.controller
 * Description:
 *
 * 参考前端：src >api>activity>activityInfo.js 编写接口
 *
 * @Author syun
 * @Create 2025/1/26 16:10
 * @Version 1.0
 */
@Api(tags = "营销活动管理")
@RestController
@RequestMapping("/admin/activity/activityInfo")
//@CrossOrigin
public class ActivityInfoController {
    @Autowired
    private ActivityInfoService activityInfoService;

    /**
     *  //列表
     * //    url: `${api_name}/${page}/${limit}`,
     * //    method: 'get'
     * @param page 当前页
     * @param limit 每页记录数
     * @return
     */
    @ApiOperation("分页查询营销活动")
    @GetMapping("{page}/{limit}")
    public Result list(@PathVariable Long page,
                       @PathVariable Long limit) {
        Page<ActivityInfo> pageParam = new Page<>(page,limit);
        IPage<ActivityInfo> pageModel =
                activityInfoService.selectPage(pageParam);
        return Result.ok(pageModel);
    }

    /**
     *  //添加活动
     * //    url: `${api_name}/save`,
     * //    method: 'post',
     * //    data: role
     * @param activityInfo 实体对象
     * @return
     */
    @ApiOperation(value = "新增活动")
    @PostMapping("save")
    public Result save(@RequestBody ActivityInfo activityInfo) {
        activityInfoService.save(activityInfo);
        return Result.ok(null);
    }

    /**
     * 修改活动
     * @param activityInfo 活动信息对象
     * @return
     */
    @ApiOperation(value = "修改活动")
    @PutMapping("update")
    public Result updateById(@RequestBody ActivityInfo activityInfo) {
        activityInfoService.updateById(activityInfo);
        return Result.ok(null);
    }

    /**
     * 删除活动
     * @param id
     * @return
     */
    @ApiOperation(value = "删除活动")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        activityInfoService.removeById(id);
        return Result.ok(null);
    }

    /**
     * 根据id列表删除活动
     * @param idList
     * @return
     */
    @ApiOperation(value="根据id列表删除活动")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<String> idList){
        activityInfoService.removeByIds(idList);
        return Result.ok(null);
    }

    //营销活动规则相关接口：分为下面的1、2、3
    /**
     *  //1 根据活动id获取活动规则数据
     * //    url: `${api_name}/findActivityRuleList/${id}`,
     * //    method: 'get'
     * @param id
     * @return
     */
    @GetMapping("findActivityRuleList/{id}")
    public Result findActivityRuleList(@PathVariable Long id) {
        Map<String,Object> activityRuleMap =
                activityInfoService.findActivityRuleList(id);
        return Result.ok(activityRuleMap);
    }

    /**
     *  //2 在活动里面添加规则数据
     * //    url: `${api_name}/saveActivityRule`,
     * //    method: 'post',
     * //    data: rule
     * @param activityRuleVo
     * @return
     */
    @PostMapping("saveActivityRule")
    public Result saveActivityRule(@RequestBody ActivityRuleVo activityRuleVo) {
        activityInfoService.saveActivityRule(activityRuleVo);
        return Result.ok(null);
    }

    /**
     *     //3 根据关键字查询匹配sku信息
     * //    url: `${api_name}/findSkuInfoByKeyword/${keyword}`,
     * //    method: 'get'
     * @param keyword
     * @return
     */
    @GetMapping("findSkuInfoByKeyword/{keyword}")
    public Result findSkuInfoByKeyword(@PathVariable("keyword") String keyword) {
        List<SkuInfo> list =
                activityInfoService.findSkuInfoByKeyword(keyword);
        return Result.ok(list);
    }


    /**
     *  //    url: `${api_name}/get/${id}`,
     * //    method: 'get'
     * @param id
     * @return
     */
    @ApiOperation(value = "获取活动")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        ActivityInfo activityInfo = activityInfoService.getById(id);
        activityInfo.setActivityTypeString(activityInfo.getActivityType().getComment());
        return Result.ok(activityInfo);
    }


}
